<?php
	//TODO FLYTTA OCH FIXA GREJER
	
	class LoginHandler{
	
		private $m_isUserLoggedIn = "isUserLoggedIn";
		private $m_UserHandler = NULL;

		public function __construct(UserHandler $aUserHandler) {
				$this->m_UserHandler = $aUserHandler;
		}
		 
		// Function to check if the user is logged in
		// Returns true if the user is logged in, and false if it isn't.
		function IsLoggedIn(){
			if (isset($_SESSION[$this->m_isUserLoggedIn])){
				if ($_SESSION[$this->m_isUserLoggedIn] == true) {
					return true;				
				}
			}
	 	 	return false;
		}
		
		// Function to log the user in. 
		// Returns true is successful, or false if it failed.
		function DoLogin($username,$password, $tempPassword){
			
			if($this->m_UserHandler->CheckLogin($username,$password) == TRUE){
				$_SESSION[$this->m_isUserLoggedIn] = true;		
				$this->m_UserHandler->SaveTempPassword($username, $tempPassword);
				return true;
			}
			
			return false;
		}
		
		function GetTempPassword()
		{
			return $this->m_UserHandler->GenerateTempPassword();
		}
		
		// Function to log out the user.
		// Returns nothing.
		function DoLogout(){
			$_SESSION[$this->m_isUserLoggedIn] = false;
		}
		
		//Function to test the LoginHandler class
		//Returns true if succesfull, throws Exception if anything fails
		function Test(){
			
			if($this->m_UserHandler->CheckIfUserExists("correctUsername", '42') == false)
			{
				Log::LogMessage("CheckIfUserExists is not working as intended.");
				return false;
			}
			if($this->m_UserHandler->CheckIfUserExists("badUsername", '42') == true)
			{
				Log::LogMessage("CheckIfUserExists is not working as intended.");
				return false;
			}
			$this->DoLogout();
			if($this->IsLoggedIn() == true)
			{
				Log::LogMessage("IsLoggedIn or DoLogout is not working as intended.");
				return false;
			}
			
			if($this->DoLogin('badUsername','badPassword', '42') == true)
			{
				Log::LogMessage("DoLogin is not working as intended. Able to log in with incorrect user data");
				return false;
			}
			
			if($this->DoLogin('correctUsername','correctPassword', '42') == false)
			{
				Log::LogMessage("DoLogin is not working as intended. Unable to log in with correct user data");
				return false;
			}
			
			if($this->IsLoggedIn() == false)
			{
				Log::LogMessage("IsLoggedIn is not working as intended.");
				return false;
			}
			
			$this->DoLogout();
			if($this->IsLoggedIn() == true)
			{
				Log::LogMessage("DoLogout is not working as intended.");
				return false;
			}
			
			if($this->DoLogin('Leif','testLeifPassword', '42') == false)
			{
				Log::LogMessage("DoLogin is not working as intended. Unable to log in with correct user data");
				return false;
			}
			$this->DoLogout();
			
			if($this->DoLogin('correctUsername','badPassword', '42') == true)
			{
				Log::LogMessage("DoLogin is not working as intended. Able to log in with incorrect user data");
				return false;
			}
			if($this->DoLogin('badUsername','correctPassword', '42') == true)
			{
				Log::LogMessage("DoLogin is not working as intended. Able to log in with incorrect user data");
				return false;
			}
			
			return true;
		}
	}
	